package com.cykj.page03;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @BelongsProject: HandTomcat
 * @BelongsPackage: com.cykj.page03
 * @Author: 阿隆
 * @CreateTime: 2025-10-25 10:16
 * @Description: 响应消息
 * @Version: 1.0
 */
public class MyHttpResponse {
    //客户端
    private Socket socket;

    public MyHttpResponse(Socket socket) {
        this.socket = socket;
    }


    /**
     * @Description: 写出消息
     * @Param: [media, fileByte]
     * @return: void
     */
    public void write(String media, byte[] fileByte) {

        try {
            //给浏览器响应消息
            OutputStream os = socket.getOutputStream();
            os.write("HTTP/1.1 200 OK".getBytes());
            os.write("\r\n".getBytes());  //回车换行
            os.write(("Content-Type: " + media + "; charset=utf-8").getBytes());   //动态获取媒体类型如何解决?
            os.write("\r\n".getBytes());  //回车换行
            os.write(("Content-Length: " + fileByte.length).getBytes()); //大小 字节大小
            os.write("\r\n".getBytes());  //回车换行
            os.write("\r\n".getBytes());  //回车换行
            os.write(fileByte);

            os.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }
}